package com.goldcn.sys.controller;

import com.goldcn.common.CommonUtils;
import com.goldcn.common.ResultData;
import com.goldcn.interceptor.CurrentUser;
import com.goldcn.sys.service.UserManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/6.
 */
@RestController
@RequestMapping(value = "/userManage")
@Api(value = "用户管理",description = "用户管理")
public class UserManageController {
    @Autowired
    UserManageService userManageService;

    @ApiOperation(value="主键查询用户信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="userId",dataType ="String",required=true,value="主键",defaultValue="2")})
    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    public Object getUser(@RequestParam String userId) {
        return userManageService.getUser(userId);
    }

    @ApiOperation(value="添加用户")
    @RequestMapping(value = "/addUser" ,method = RequestMethod.POST)
    public Object addUser(@RequestBody com.goldcn.common.RequestParam  params, HttpServletRequest request) throws Exception {
        Map<String,String> map = (Map<String,String>) params.getParams();
        return userManageService.addUser(map,request);
    }

    @ApiOperation(value="修改用户")
    @RequestMapping(value = "/updateUser" ,method = RequestMethod.POST)
    public Object updateUser(@RequestBody com.goldcn.common.RequestParam  params, HttpServletRequest request) throws Exception {
        Map<String,String> map = (Map<String,String>) params.getParams();
        return userManageService.updateUser(map,request);
    }

    @ApiOperation(value="停用或恢复正常")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="userId",dataType ="String",required=true,value="主键",defaultValue="2")})
    @RequestMapping(value = "/switch", method = RequestMethod.GET)
    public Object stopUse(@RequestParam String userId, @RequestParam String stop) {
        return userManageService.stopOrUse(userId,stop);
    }

    @ApiOperation(value="删除用户")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="userId",dataType ="String",required=true,value="用户ID",defaultValue="2")})
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public Object deleteUser(@RequestParam String userId) {
        return userManageService.deleteUser(userId);
    }

    @ApiOperation(value="用户管理分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="pageIndex",dataType ="int",required=true,value="从第几条开始",defaultValue="1"),
            @ApiImplicitParam(paramType="query",name="pageSize",dataType ="int",required=true,value="每页多少条",defaultValue="5"),
            @ApiImplicitParam(paramType="query",name="sortField",dataType ="String",required=false,value="排序字段",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="sortOrder",dataType ="String",required=false,value="排序方式（asc，desc）",defaultValue="asc")
    })
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    public Map get(HttpServletRequest request, HttpServletResponse response){
        Map<String, String> params = CommonUtils.getParameterMap(request);
        return userManageService.selectAll(params);
    }

    //当前用户信息（获取中文名、公司名称等数据,首页展示）
    @RequestMapping(value = "/currUserInfo", method = RequestMethod.GET)
    public Object getAccount(HttpServletRequest request){
        return userManageService.currUserInfo(request);
    }

    //检测用户有没登入
    @RequestMapping(value = "/user/checkLogin", method = RequestMethod.GET)
    public Object checkLogin(HttpServletRequest request){
        ResultData<CurrentUser> result = new ResultData();
        CurrentUser user = (CurrentUser)request.getAttribute("user");
        result.setData(user);
        return user;
    }
}


